package com.elong.android.specialhouse.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.elong.android.specialhouse.R;


/**
 * @author 自定义带清空按钮的EditText
 */
public class ClearableEditText extends EditText implements View.OnFocusChangeListener,
        TextWatcher {


    private Context mContext;
    private Drawable mDrawableRight;
    private boolean hasFocus = false;

    private TextWatcherListener mTextWatcherListener;

    public TextWatcherListener getTextWatcherListener() {
        return mTextWatcherListener;
    }

    public void setTextWatcherListener(TextWatcherListener textWatcherListener) {
        this.mTextWatcherListener = textWatcherListener;
    }

    public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public ClearableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ClearableEditText(Context context) {
        super(context);
        init(context, null);
    }


    private void init(Context context, AttributeSet attrs) {
        this.mContext = context;
        mDrawableRight = getCompoundDrawables()[2];
        if (mDrawableRight == null) {
            mDrawableRight = getResources().getDrawable(R.drawable.icon_clear_grey);
        }

        resetClearIcon(hasFocus, getText().length() > 0);
        // 设置焦点改变的监听
        setOnFocusChangeListener(this);
        // 设置输入框里面内容发生改变的监听
        addTextChangedListener(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                if (mDrawableRight != null) {
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    if (getText().length() > 0
                            && (x < getWidth())
                            && (x > getWidth() - getTotalPaddingRight())
                            && (y > 0)
                            && (y < getHeight())) {
                        setText("");
                        resetClearIcon(hasFocus, getText().length() > 0);
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFocus = hasFocus;
        resetClearIcon(hasFocus, getText().length() > 0);
    }

    /**
     * 重置清除按钮的状态
     *
     * @param hasFocus
     * @param hasContent
     */
    private void resetClearIcon(boolean hasFocus, boolean hasContent) {
        if (mDrawableRight != null) {
            setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], getCompoundDrawables()[1], hasFocus && hasContent ? mDrawableRight : null, getCompoundDrawables()[3]);
        }
    }


    @Override
    public void afterTextChanged(Editable s) {
        resetClearIcon(hasFocus, getText().length() > 0);
        if (mTextWatcherListener != null) {
            mTextWatcherListener.afterTextChanged(s);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        if (mTextWatcherListener != null) {
            mTextWatcherListener.beforeTextChanged(s, start, count, after);
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (mTextWatcherListener != null) {
            mTextWatcherListener.onTextChanged(s, start, before, count);
        }
    }

    public interface TextWatcherListener {
        void afterTextChanged(Editable s);

        void beforeTextChanged(CharSequence s, int start, int count, int after);

        void onTextChanged(CharSequence s, int start, int before, int count);
    }
}
